<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix='sec' uri='http://www.springframework.org/security/tags'%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script type="text/javascript" src="scripts/boot.js"></script>
</head>
<body style="border: 0px; margin: 0px; padding: 0px;width: 100%;height: 100%">
	<div class="mini-toolbar" style="border-bottom:0;padding:0px;">
	    <table style="width:100%;">
	        <tr>
	        	<td style="width:100%;">
	        	 <sec:authorize url='/purchasing_power_fi_center.do'>
	        		<a class="mini-button" iconCls="icon-redo" onclick="onUser" plain="true">批量分配</a>
	        	</sec:authorize>
	        	 <sec:authorize url='/buy_frequency_export_all.do'>
	        		<a class="mini-button" iconCls="icon-download" plain="true" onclick="exportAll()">导出</a>
	        	</sec:authorize>
	        	</td>
	            <td style="white-space:nowrap;">
	            	选择时间段：
	        		<input id="start_time" class="mini-monthpicker" format="yyyy-MM" tooltip="请选择开始日期" showClose="true" oncloseclick="clearT" allowInput="false" style="width:100px;"/>
	        		&nbsp;—&nbsp;
	        		<input id="end_time" class="mini-monthpicker" format="yyyy-MM" tooltip="请选择截止日期" showClose="true" oncloseclick="clearT" allowInput="false" style="width:100px;"/>
					<a class="mini-button" iconCls="icon-search" onclick="search()">查询</a>
	            </td>
	        </tr>
	    </table>
	</div>
	
	<div class="mini-fit" >
	<div id="datagrid1" class="mini-datagrid" idField="id" pageSize="20"
		url="buy_frequency_basic_view.do" multiSelect="true" style="width:100%;height:100%;">
		<div property="columns">
			<div type="checkcolumn"></div>
			<div type="indexcolumn" width="5%" headerAlign="center">排名</div>
			<div field="name" width="20%" headerAlign="center" align="center">姓名</div>
			<div field="countBuy" width="25%" headerAlign="center" align="center">购买次数</div>
			<div field="sexValue" width="25%" headerAlign="center" align="center">性别</div>
			<div field="phoneNum" width="25%" headerAlign="center" align="center">联系电话</div>
		</div>
	</div>
	</div>
	
	<div id="win1" class="mini-window" allowDrag="false" allowResize="false" showHeader="true" title="话务分配--选择话务员" style="width: 600px;height: 360px;">
    	 <div style="text-align:center;line-height:23px;padding:5px;">
	         <span>选择中心：</span>    
	         <input emptyText="请选择中心" class="mini-combobox" style="width:180px;"
		 		id="call_user_center" textField="name" valueField="id" url="" onvaluechanged="loadGroups();"/>
		 	<span style="margin-left: 10px;">选择话务组：</span>
	         <input emptyText="请选择话务组" class="mini-combobox" style="width:180px;"
		 		id="call_user_group" textField="name" valueField="id" url="" onvaluechanged="loadUsers();"/>
    	 </div>
		<div style="padding-left:5px;padding-right:5px;">
	        <table cellpadding="0" cellspacing="0" >
	            <tr>
	                <td >
	                    <h4 style="margin:0;line-height:22px;font-size:13px;">话务员列表：</h4>
	                    <div id="call_user" class="mini-datagrid" style="width:200px;height:200px;" showPageSize="false" showPageIndex="false"
	                        pagerStyle="padding:2px;" multiSelect="true" url="purchasing_power_find_user.do" showReloadButton="false" showPager="false">
	                        <div property="columns">
	                            <div type="checkcolumn" ></div>
	                            <div field="name" width="100" headerAlign="center" >话务员</div>
	                        </div>
	                    </div>
	                </td>
	                <td style="padding:5px;">
	           			<input type="button" value="选择" onclick="addSelectedUser()" style="width:50px;"/><br />
	                </td>
	                <td >
	                    <h4 style="margin:0;line-height:22px;font-size:13px;">已选话务员：</h4>
	                    <div id="selectedUser" class="mini-listbox" style="width:200px;height:200px;"              
	                            showCheckBox="true" multiSelect="true" >     
	                        <div property="columns">
	                            <div field="name" width="100" headerAlign="center" >话务员</div>    
	                        </div>
	                    </div>                       
	                </td>
	                <td style="padding:5px;">
			            <input type="button" value="删除" onclick="removeUsers()" style="width:55px;margin-bottom:2px;"/><br />                
			            <input type="button" value="清空" onclick="removeAllUsers()" style="width:55px;"/><br />                
	                </td>
	            </tr>
	        </table>
	    </div>
		<div style="text-align:center;padding: 20px;">
			<a class="mini-button" onclick="onOk1" style="">确定</a>
			<a class="mini-button" onclick="onClose1" style="margin-left: 50px;">关闭</a>
		</div>
	</div>
	
<script type="text/javascript">
	mini.parse();
	var grid = mini.get("datagrid1");
	var startTime = mini.get("start_time");
	var endTime = mini.get("end_time");
	var myDate = new Date();
	var sm = myDate.getFullYear() + '-'+myDate.getMonth()+"";
	var em = myDate.getFullYear() + '-'+ (myDate.getMonth()+1+"");
	startTime.setValue(sm);
	endTime.setValue(em);
	grid.load({startTime: startTime.getFormValue(), endTime: endTime.getFormValue()});
	
	function search() {
		grid.load({startTime: startTime.getFormValue(), endTime: endTime.getFormValue()});
	}
	
	//导出所有
	function exportAll(){
		var startTime = mini.get("start_time").getFormValue();
		var endTime = mini.get("end_time").getFormValue();
		
	    location.href="buy_frequency_export_all.do?startTime="+startTime+"&endTime="+endTime;
	}
	
	function clearT(e){
		e.sender.setValue("");	
	}
	
	
	//--------------------------------  批量分配
	function getSelectRows(){
		var rows = grid.getSelecteds();
		var id = [];
		if(rows.length > 0){
			var ids = [];
	           for (var i = 0, l = rows.length; i < l; i++) {
	               var r = rows[i];
	               ids.push(r.id);
	           }
	           id = ids.join(',');
		}
		return id;
	}

	var win1 = mini.get("win1");
	var callUserCenter = mini.get("call_user_center");
	var callUserGroup = mini.get("call_user_group");
	function onUser(e){
		var callIds = getSelectRows();
		if(callIds.length > 0) {
			var el=e.sender.getEl();
			callUserCenter.load("purchasing_power_find_center.do");
			win1.showAtEl(el, {
			   xAlign:"center",
			   yAlign:"middle"
			});
		}else {
			mini.alert("未选择任何数据");
		}
	}

	function loadGroups() {
		callUser.clearRows();
		callUserGroup.load("purchasing_power_find_group.do?centerId=" + callUserCenter.getValue());
	}

	var callUser = mini.get("call_user");
	var selectedUser = mini.get("selectedUser");
	function loadUsers() {
		callUser.load({groupId: callUserGroup.getValue()});
	}

	function GetUserData() {
	    var rows = selectedUser.getData();
	    var ids = [], texts = [];
	    for (var i = 0, l = rows.length; i < l; i++) {
	        var row = rows[i];
	        ids.push(row.id);
	        texts.push(row.name);
	    }

	    var data = {};
	    data.id = ids.join(",");
	    data.text = texts.join(",");
	    return data;
	}

	function addSelectedUser() {
	    var items = callUser.getSelecteds();

	    //根据id属性，来甄别要加入选中的记录
	    var idField = callUser.getIdField();

	    //把已选中的数据，用key-value缓存，以便进一步快速匹配
	    var idMaps = {};
	    var selecteds = selectedUser.getData();
	    for (var i = 0, l = selecteds.length; i < l; i++) {
	        var o = selecteds[i];
	        var id = o[idField];
	        idMaps[id] = o;
	    }

	    //遍历要加入的数组
	    for (var i = items.length - 1; i >= 0; i--) {
	        var o = items[i];
	        var id = o[idField];
	        if (idMaps[id] != null) items.removeAt(i);
	    }
	    selectedUser.addItems(items);
	}

	function removeUsers() {
	    var items = selectedUser.getSelecteds();
	    selectedUser.removeItems(items);
	}

	function removeAllUsers() {
	    selectedUser.removeAll();
	}

	function onOk1() {
		var callIds = getSelectRows();
		var selectedUsers = GetUserData();
		$.post("call_allocate_user.do", {id: callIds, handleUserId: selectedUsers.id}, function(data){
			if(data.success) {
				mini.showMessageBox({
	                title: "提示",
	                showModal:false,
	                iconCls: "mini-messagebox-info",
	                message: "分配成功！",
	                timeout: 3000,
	                x: 'right',
	                y: 'bottom',
	            });
				grid.reload();
				onClose1(e);
			}else {
				mini.alert("数据错误（待分配任务id或话务员id不能为空）");
			}
		}, "json");
	}

	function onClose1(e){
		callUser.clearRows();
		selectedUser.removeAll();
		callUserCenter.setValue("");
		callUserGroup.setValue("");
		win1.hide();
	}
</script>
</body>
</html>